-- ============================================================================
-- 添加 DataStatus 字段到所有继承 Audit 的表
-- 执行日期: 2025-10-29
-- 说明: 为支持"保存草稿"功能，在所有业务表中添加数据状态字段
-- ============================================================================

USE [WMS仓储管理系统]; -- 请根据实际数据库名称修改
GO

-- 1. StoragelocationInfo 表
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'StoragelocationInfo') AND name = 'DataStatus')
BEGIN
    ALTER TABLE [dbo].[StoragelocationInfo]
    ADD [DataStatus] INT NOT NULL DEFAULT 2;
    
    PRINT '✓ StoragelocationInfo 表已添加 DataStatus 字段';
END
ELSE
BEGIN
    PRINT '× StoragelocationInfo 表的 DataStatus 字段已存在';
END
GO

-- 2. WarehouseInfo 表
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'WarehouseInfo') AND name = 'DataStatus')
BEGIN
    ALTER TABLE [dbo].[WarehouseInfo]
    ADD [DataStatus] INT NOT NULL DEFAULT 2;
    
    PRINT '✓ WarehouseInfo 表已添加 DataStatus 字段';
END
ELSE
BEGIN
    PRINT '× WarehouseInfo 表的 DataStatus 字段已存在';
END
GO

-- 3. HandlerInfo 表
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'HandlerInfo') AND name = 'DataStatus')
BEGIN
    ALTER TABLE [dbo].[HandlerInfo]
    ADD [DataStatus] INT NOT NULL DEFAULT 2;
    
    PRINT '✓ HandlerInfo 表已添加 DataStatus 字段';
END
ELSE
BEGIN
    PRINT '× HandlerInfo 表的 DataStatus 字段已存在';
END
GO

-- 4. InventoryInfo 表
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'InventoryInfo') AND name = 'DataStatus')
BEGIN
    ALTER TABLE [dbo].[InventoryInfo]
    ADD [DataStatus] INT NOT NULL DEFAULT 2;
    
    PRINT '✓ InventoryInfo 表已添加 DataStatus 字段';
END
ELSE
BEGIN
    PRINT '× InventoryInfo 表的 DataStatus 字段已存在';
END
GO

-- 5. Inventory_check_details 表
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'Inventory_check_details') AND name = 'DataStatus')
BEGIN
    ALTER TABLE [dbo].[Inventory_check_details]
    ADD [DataStatus] INT NOT NULL DEFAULT 2;
    
    PRINT '✓ Inventory_check_details 表已添加 DataStatus 字段';
END
ELSE
BEGIN
    PRINT '× Inventory_check_details 表的 DataStatus 字段已存在';
END
GO

-- 6. ProductInfo 表
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'ProductInfo') AND name = 'DataStatus')
BEGIN
    ALTER TABLE [dbo].[ProductInfo]
    ADD [DataStatus] INT NOT NULL DEFAULT 2;
    
    PRINT '✓ ProductInfo 表已添加 DataStatus 字段';
END
ELSE
BEGIN
    PRINT '× ProductInfo 表的 DataStatus 字段已存在';
END
GO

-- 7. StorageWarehouse 表
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'StorageWarehouse') AND name = 'DataStatus')
BEGIN
    ALTER TABLE [dbo].[StorageWarehouse]
    ADD [DataStatus] INT NOT NULL DEFAULT 2;
    
    PRINT '✓ StorageWarehouse 表已添加 DataStatus 字段';
END
ELSE
BEGIN
    PRINT '× StorageWarehouse 表的 DataStatus 字段已存在';
END
GO

-- 8. Uploadattachment 表
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'Uploadattachment') AND name = 'DataStatus')
BEGIN
    ALTER TABLE [dbo].[Uploadattachment]
    ADD [DataStatus] INT NOT NULL DEFAULT 2;
    
    PRINT '✓ Uploadattachment 表已添加 DataStatus 字段';
END
ELSE
BEGIN
    PRINT '× Uploadattachment 表的 DataStatus 字段已存在';
END
GO

-- 9. WarehouseDept 表
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'WarehouseDept') AND name = 'DataStatus')
BEGIN
    ALTER TABLE [dbo].[WarehouseDept]
    ADD [DataStatus] INT NOT NULL DEFAULT 2;
    
    PRINT '✓ WarehouseDept 表已添加 DataStatus 字段';
END
ELSE
BEGIN
    PRINT '× WarehouseDept 表的 DataStatus 字段已存在';
END
GO

-- 10. WarehouseSupervisor 表
IF NOT EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'WarehouseSupervisor') AND name = 'DataStatus')
BEGIN
    ALTER TABLE [dbo].[WarehouseSupervisor]
    ADD [DataStatus] INT NOT NULL DEFAULT 2;
    
    PRINT '✓ WarehouseSupervisor 表已添加 DataStatus 字段';
END
ELSE
BEGIN
    PRINT '× WarehouseSupervisor 表的 DataStatus 字段已存在';
END
GO

-- ============================================================================
-- 数据状态说明
-- ============================================================================
-- 1: 草稿（用户保存草稿时使用）
-- 2: 已提交/正式数据（默认值）
-- 3: 审核中（如果有审核流程）
-- 4: 已审核通过

PRINT '';
PRINT '====================================';
PRINT '数据库迁移完成！';
PRINT 'DataStatus 字段已成功添加到所有表';
PRINT '====================================';
GO

